/**
 * @author Stephan Pfaff (http://www.derdaumen.net)
 */

/**
 * The Category Object
 * @param vars object providing the category properties (loaded from the database)
 * @return
 */

function Category (vars, params, parent){
	if (vars != null) {
		try {
			Item.call(this, vars.prefix, vars.id, vars.type, vars.basename, vars.path, vars.label, vars.alias, vars.thumb, vars.thumb_enable, false, vars.item_class);
			this.start_x 	= parseInt(vars.start_x)*site.gridSize;
			this.start_y 	= parseInt(vars.start_y)*site.gridSize;
			this.position 	= vars.position*site.gridSize;
			this.bg_active 	= '#0f0';
			this.bg_inactive = '#232323';
			
			//check ob das menü item aktiv ist, entsprechende klasse zuweisen
			var div_class = params.ctg_id == this.id ? 'item '+this.prefix+' active' : 'item '+this.prefix;
			//absolute positionierung im preface, oder auf der hauptseite berechnen
			if (parent.type == 'preface') {
				var style = 'left: '+this.start_x+'px; top: '+this.start_y+'px;';
			} else {
				var style = 'left: '+this.position+'px;';
			}
			
			//Überprüfung ob das element per php oder javascript erstellt wird
			if(noPHPMode) {
			//seite läuft koplett über AJAX, element muss per javascript erstellt werden
				var div_id = this.prefix+"_"+this.id;
				var html 		= ["<div id='"+div_id+"'"
				+"class='"+div_class+"'"
				+"style='"+style+"'>\n"];
				if(this.thumb != "" && this.thumb_enable) html.push("	<img id='thumb_"+div_id+"' class='thumb' src='"+this.thumb+"' alt='"+this.alias+"'/>\n");
				if(this.label != "") html.push("	<div id='label_"+div_id+"' class='label'>"+this.label+"</div>\n");
				html.push("</div>\n");
				this.$div = $(html.join(''));
				parent.$scrollWrapper.append(this.$div);
			} else {
				this.$div = $("#"+this.prefix+"_"+this.id);
			}
			//id als data attribut einfügen, um im noPHPMode bei einem klick
			//das element im neu erstellten menü wieder aktiv setzen zu können
			this.$div.data('id', this.id);
			
			//bei aktivem noPHPMode das Element draggable machen
			if(noPHPMode && allowDraggable)
				this.$div.draggable({
					revert: false,
					cancel: '.active',
					zIndex: 100
				});
			
			//content laden, falls das element aktiv ist
			if(this.id == params.ctg_id) {
				this.loadChilds(params);
			}
			//zeiger auf das objekt um auch innerhalb von inline methoden darauf zugreifen zu können
			var handle = this;
			
			//hover
			this.$div.hover(
				function(event) {
					if(!handle.locked && !$(this).hasClass("active"))
					$(this).stop().animate({
						backgroundColor: handle.bg_active
					}, site.navAnimationDuration);
				},
				function(event) {
					if(!handle.locked && !$(this).hasClass("active"))
					$(this).stop().animate({
						backgroundColor: handle.bg_inactive
					}, site.navAnimationDuration);
			});
			
			//click
			this.$div.click(function(event) {
				if(noPHPMode) {
					//wenn preface angezeigt wird
					if(site.preface != null) {
						var targetID = $(event.target).data('id');
						site.preface.$div.fadeOut(site.containerFadeDuration, function() {
							$(this).remove();
							site.preface = null;
							site.categories.load({prefix: 'ctg', ctg_id: targetID}, false);
							$('#main').after('<div id="footer" class="footer" style="display: none;">'
									+'	<a href="index.php" target="_self">'
									+'		<img src="css/frontend/screen/images/logo.png" alt="logo" />'
									+'	</a>'
									+'</div>');
							var $footer = $('#footer');
							$footer.fadeIn(site.containerFadeDuration);
						});
					//wenn die hauptseite angezeigt wird
					} else if(!$(this).hasClass("active")) {
							handle.loadChilds(params);
						site.categories.$div.find(".active").animate({
								backgroundColor: handle.bg_inactive
						}, site.navAnimationDuration).removeClass('active');
						$(this).addClass("active");
					} else {
						handle.loadChilds(params);
					}
				}
			});
		} catch(e) {
			if(debug) alert('Initialization of Category failed, missing or wrong parameter! ('+e+')');
		}
	}
};
Category.prototype = new Item();
Category.prototype.start_x = 0;
Category.prototype.start_y = 0;
Category.prototype.position = 0;
Category.prototype.$div = null;
Category.prototype.bg_active = '#0f0';
Category.prototype.bg_inactive = '#232323';

/**
 * Loads the category's children.
 */
Category.prototype.loadChilds = function(params) {
	if(noPHPMode){ 
		params = {ctg_id: this.id};
	}
	site.projects.load(params, null, false);
	if(params.prj_id == null)
		site.buttons.load(params, null, false);
};